package com.haojiang.mh.remote.adapter

import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.enums.EConnect
import com.haojiang.mh.remote.utils.BleUtil.Companion.groupData
import com.haojiang.mh.remote.utils.Util.Companion.playSound
import com.haojiang.mh.remote.widget.GroupTypeItemView
import kotlinx.android.synthetic.main.item_group_type.view.*
import java.util.*

class GroupTypeDialogFragAdapter :
    RecyclerView.Adapter<GroupTypeDialogFragAdapter.ViewHolder>() {

    private var dataList = arrayListOf<Int>(
        EConnect.CONNECT_GROUP_SINGLE.data,
        EConnect.CONNECT_GROUP_MULTIPLE.data,
        EConnect.CONNECT_GROUP_SPLIT.data
    )

    private val viewList = ArrayList<GroupTypeItemView>()
    private var grayColor: Int = 0
    private var whiteColor: Int = 0
    private var bgSelectColor = 0
    private var bgUnselectColor = 0
    var curGroupType = EConnect.CONNECT_GROUP_SINGLE.data


    init {
        grayColor = ContextCompat.getColor(AppContext.instance(), R.color.color_gray_dark)
        whiteColor = ContextCompat.getColor(AppContext.instance(), R.color.color_white)
        bgSelectColor = ContextCompat.getColor(AppContext.instance(), R.color.color_black_light3)
        bgUnselectColor = ContextCompat.getColor(AppContext.instance(), R.color.color_black_light2)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
//        val groupTypeItemView = GroupTypeItemView(parent.context)
        return ViewHolder(GroupTypeItemView(parent.context))       //BleItemView是layout文件
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemView = holder.itemView as GroupTypeItemView
        if (!viewList.contains(itemView)) {
            viewList.add(holder.itemView)
            when (dataList.get(position)) {
                EConnect.CONNECT_GROUP_SINGLE.data -> {
                    itemView.ivIcon.setImageResource(R.drawable.ic_group_single_p)
                    itemView.tvName.setText(holder.itemView.context.getString(R.string.f_single_control))
                    itemView.tvName.setTextColor(whiteColor)
                    itemView.cardView.setCardBackgroundColor(bgSelectColor)
                    itemView.groupType = EConnect.CONNECT_GROUP_SINGLE.data
                }

                EConnect.CONNECT_GROUP_MULTIPLE.data -> {
                    itemView.ivIcon.setImageResource(R.drawable.ic_group_multiple_n)
                    itemView.tvName.setText(holder.itemView.context.getString(R.string.f_multiple_control))
                    itemView.tvName.setTextColor(grayColor)
                    itemView.cardView.setCardBackgroundColor(bgUnselectColor)
                    itemView.groupType = EConnect.CONNECT_GROUP_MULTIPLE.data
                }

                EConnect.CONNECT_GROUP_SPLIT.data -> {
                    itemView.ivIcon.setImageResource(R.drawable.ic_group_split_n)
                    itemView.tvName.setText(holder.itemView.context.getString(R.string.f_spilit_control))
                    itemView.tvName.setTextColor(grayColor)
                    itemView.cardView.setCardBackgroundColor(bgUnselectColor)
                    itemView.groupType = EConnect.CONNECT_GROUP_SPLIT.data
                }
            }

            itemView.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    playSound()
                    curGroupType = itemView.groupType
                    groupData.groupKey.type = itemView.groupType
                    setBg(v as GroupTypeItemView)
                }
            })
        }
    }

    private fun setBg(itemView: GroupTypeItemView) {
        viewList.forEach {
            when (it.groupType) {
                EConnect.CONNECT_GROUP_SINGLE.data -> {
                    if (it.equals(itemView)) {
                        it.ivIcon.setImageResource(R.drawable.ic_group_single_p)
                    } else {
                        it.ivIcon.setImageResource(R.drawable.ic_group_single_n)
                    }
                }
                EConnect.CONNECT_GROUP_MULTIPLE.data -> {
                    if (it.equals(itemView)) {
                        it.ivIcon.setImageResource(R.drawable.ic_group_multiple_p)
                    } else {
                        it.ivIcon.setImageResource(R.drawable.ic_group_multiple_n)
                    }
                }
                EConnect.CONNECT_GROUP_SPLIT.data -> {
                    if (it.equals(itemView)) {
                        it.ivIcon.setImageResource(R.drawable.ic_group_split_p)
                    } else {
                        it.ivIcon.setImageResource(R.drawable.ic_group_split_n)
                    }
                }
            }
            it.tvName.setTextColor(grayColor)
            it.cardView.setCardBackgroundColor(bgUnselectColor)
        }
        itemView.tvName.setTextColor(whiteColor)
        itemView.cardView.setCardBackgroundColor(bgSelectColor)
    }

    fun clearData() {
        viewList.clear()
    }

//    private fun clickItemDelay(view: View, delayMs: Int) {
//        view.isEnabled = false
//        view.postDelayed({ view.isEnabled = true }, delayMs.toLong())
//    }

//    fun dataRefresh() {
//        Collections.sort(dataList)
//        notifyDataSetChanged()
//    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    }
}